package com.hotec.PROJECT_NAME.config.encrypt;

import com.hotec.PROJECT_NAME.utils.Utils3DES;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
@Aspect
public class ApiEncryptAspect {
    @Around("@annotation(apiEncrypt)")
    public Object doAroundAdvice(ProceedingJoinPoint point, ApiEncrypt apiEncrypt) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

        Object[] args = point.getArgs();

        args[apiEncrypt.index()] = Utils3DES.decryptWithRequest(request, apiEncrypt.type());

        return point.proceed(args);
    }
}
